package com.iiiss.template.ssh.impl.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.iiiss.template.ssh.core.beans.ISnsUser;
import com.iiiss.template.ssh.core.services.ISnsUserService;

/*
 * TemplateSSH - com.iiiss.template.ssh.impl.controllers - SnsUserController.java
 * 
 * Copyright (C) 2011  Mingye Cheng <Cheng, Mingye at GMAIL>
 * 
 * SnsUserController.java is part of <iiiStudio Template Framework> project.
 * This project is hosted at <http://iiiss-template-ssh.googlecode.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Revision history:
 * [2011-8-25] Mingye Cheng - Initial version established,
 * [2011-8-27] Mingye Cheng - Using Exception mechanism for non-existent users,
 *                            Useless annotations removed,
 *                            Interface 'friend/search' added.
 */
@Controller
public class SnsUserController {
	public static final String RESULT_PAGE = "snsuser";

	public static final String MESSAGE_VAR = "message";
	public static final String FRIEND_VAR = "friend";
	public static final String FOLLOWER_VAR = "follower";
	public static final String USERS_VAR = "users";

	public static final String BAD_REQUEST = "Bad Request.";
	public static final String NO_SUCH_USER = "No such user.";

	@Autowired
	private ISnsUserService snsUserService;

	@RequestMapping(value = "/friend/relationship")
	public String relationship(@RequestParam String current,
			@RequestParam String target, Model model) {
		if (current == null || target == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		try {
			boolean isFriend = snsUserService.isFriend(current, target);
			boolean isFollower = snsUserService.isFriend(target, current);

			model.addAttribute(FRIEND_VAR, isFriend);
			model.addAttribute(FOLLOWER_VAR, isFollower);
			return RESULT_PAGE;

		} catch (NullPointerException e) {
			model.addAttribute(MESSAGE_VAR, NO_SUCH_USER);
			return RESULT_PAGE;
		}
	}

	@RequestMapping(value = "/friend/add")
	public String add(@RequestParam String current,
			@RequestParam String target, Model model) {
		if (current == null || target == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		try {
			boolean isSucceed = snsUserService.addFriend(current, target);

			model.addAttribute(FRIEND_VAR, isSucceed);
			return RESULT_PAGE;

		} catch (NullPointerException e) {
			model.addAttribute(MESSAGE_VAR, NO_SUCH_USER);
			return RESULT_PAGE;
		}
	}

	@RequestMapping(value = "/friend/count")
	public String count(@RequestParam String current,
			@RequestParam(required = false) String target, Model model) {
		if (current == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		try {
			int friends = snsUserService.countFriends(current, target);
			int followers = snsUserService.countFollowers(current, target);

			model.addAttribute(FRIEND_VAR, friends);
			model.addAttribute(FOLLOWER_VAR, followers);
			return RESULT_PAGE;

		} catch (NullPointerException e) {
			model.addAttribute(MESSAGE_VAR, NO_SUCH_USER);
			return RESULT_PAGE;
		}
	}

	@RequestMapping(value = "/friend/friends")
	public String getFriends(@RequestParam String current,
			@RequestParam(required = false) String target, Model model) {
		if (current == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		try {
			List<ISnsUser> users = snsUserService.getFriends(current, target);

			model.addAttribute(USERS_VAR, users);
			return RESULT_PAGE;

		} catch (NullPointerException e) {
			model.addAttribute(MESSAGE_VAR, NO_SUCH_USER);
			return RESULT_PAGE;
		}
	}

	@RequestMapping(value = "/friend/followers")
	public String getFollowers(@RequestParam String current,
			@RequestParam(required = false) String target, Model model) {
		if (current == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		try {
			List<ISnsUser> users = snsUserService.getFollowers(current, target);

			model.addAttribute(USERS_VAR, users);
			return RESULT_PAGE;

		} catch (NullPointerException e) {
			model.addAttribute(MESSAGE_VAR, NO_SUCH_USER);
			return RESULT_PAGE;
		}
	}

	@RequestMapping(value = "/friend/remove")
	public String remove(@RequestParam String current,
			@RequestParam String target, Model model) {
		if (current == null || target == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		try {
			boolean isSucceed = snsUserService.removeFriend(current, target);

			model.addAttribute(FRIEND_VAR, isSucceed);
			return RESULT_PAGE;

		} catch (NullPointerException e) {
			model.addAttribute(MESSAGE_VAR, NO_SUCH_USER);
			return RESULT_PAGE;
		}
	}

	@RequestMapping(value = "/friend/search")
	public String search(@RequestParam String current,
			@RequestParam(required = false) String target,
			@RequestParam String keyword, Model model) {
		if (current == null || keyword == null) {
			model.addAttribute(MESSAGE_VAR, BAD_REQUEST);
			return RESULT_PAGE;
		}

		try {
			List<ISnsUser> users = snsUserService.searchFriends(current,
					target, keyword);

			model.addAttribute(USERS_VAR, users);
			return RESULT_PAGE;

		} catch (NullPointerException e) {
			model.addAttribute(MESSAGE_VAR, NO_SUCH_USER);
			return RESULT_PAGE;
		}
	}
}
